package com.manage.base;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import com.manage.R;
import com.manage.http.BaseRequest;
import com.manage.model.Order;
import com.manage.sharedpreferences.BaseSharedPreferences;
import com.manage.sharedpreferences.SPConfig;

import android.app.Activity;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

public abstract class BaseListActivity extends ListActivity {

	private static String TAG = "BaseActivity";
	
	/**
	 * 用于登录失败提示
	 */
	public static final int TIP = 1;  
	
	/**
	 * 用于退出失败提示
	 */
	public static final int QUITTIP = 2;  
	
	/**
	 * 成功获取网络数据
	 */
	public static final int SUCCESS = 3;  
	
	/**
	 * 获取网络数据失败
	 */
	public static final int FAIL = 4;  

	/**
	 * 成功取消订单
	 */
	public static final int CANCELSUCCESS = 5;  
	
	/**
	 * 取消订单失败
	 */
	public static final int CANCELFAIL = 6;  

	/**
	 * 成功受理订单
	 */
	public static final int ACCEPTSUCCESS = 7;  
	
	/**
	 * 受理订单失败
	 */
	public static final int ACCEPTFAIL = 8;  

	/**
	 * 成功配送订单
	 */
	public static final int DELIVERYSUCCESS = 9;  
	
	/**
	 * 配送订单失败
	 */
	public static final int DELIVERYFAIL = 10;  
	
	/**
	 * 配送订单
	 */
	public static final int DELIVERYORDER = 11;  
	
	/**
	 * 入账
	 */
	public static final int ACCOUNTRDER = 12; 

	/**
	 * 入账成功
	 */
	public static final int ACCOUNTRDERSUCCESS = 13; 

	/**
	 * 入账失败
	 */
	public static final int ACCOUNTRDERFAIL = 14; 
	
	/**
	 * 获取商店信息成功
	 */
	public static final int GETSHOPINFOSUCCESS = 15;

	/**
	 * 获取商店信息失败
	 */
	public static final int GETSHOPINFOFAIL = 16;

	/**
	 * 修改订单状态成功
	 */
	public static final int MODIFYORDERSUCCESS = 17;

	/**
	 * 修改订单状态失败
	 */
	public static final int MODIFYORDERFAIL = 18;
	
	
    public static String URL = "http://3canle.com:8080/";
    
    /**
     * 登录url
     */
    public static String LOGINURL = "http://3canle.com:8080/api/shop/signin/shopname_pass.do?";
    
    /**
     * 退出url
     */
    public static String LOGOUTURL = "http://3canle.com:8080/api/shop/quit/token_shopid.do";
    
    /**
     * 获取订单列表url
     * 
     * 1:待受理订单，2：待配送订单，3：待入账订单，4：已完成,5:已取消
     */
    public static String ORDERSURL = "http://3canle.com:8080/api/order/accept.do?"; 
    
    /**
     * 修改订单状态
     * 
     * 订单状态码 1: 待受理，2：待配送，3：待入账，4：已入账,5:已取消
     */
    public static String MODIFYORDERURL = "http://3canle.com:8080/api/order/status.do?";
    
    /**
     * 今日账目统计
     */
    public static String ACCOUNTTODAYURL = "http://3canle.com:8080/api/account/statistics/today.do?";
    
    /**
     * 过去30天账目统计
     */
    public static String ACCOUNTMONTHURL = "http://3canle.com:8080/api/account/statistics/month.do?";

    /**
     * 订单评价列表
     */
    public static String EVALUATIONURL = "http://3canle.com:8080/api/order/evaluate.do?";
    
    /**
     * intent传递对象标志
     */
    public final static String SER_KEY = "com.manage.ser"; 
    
	protected Button mLeftBtn; // title左边按钮
	protected TextView mTextViewTitle; // title标题
	
	protected Context mContext;
	private boolean mState;
	  
	protected String accessToken = "0";  // 用户访问令牌
	
	/**
	 * 程序设置
	 */
	public BaseSharedPreferences sp = null;
	
	/**
	 * 处理activity的退出
	 */
    public static List<Activity> sAllActivity = new LinkedList<Activity>(); 
    
	/**
	 * 当前activity所持有的所有请求
	 */
	protected List<BaseRequest> requestList = null;

	/**
	 * 设置左边button的text
	 * @return
	 */
	public abstract String getLeftBtnName(); 
	
	/**
	 * 设置title的text
	 * @return
	 */
	public abstract String getTitleName();  
	
	/**
	 * 设置是否显示左边按钮
	 * @return
	 */
	public abstract boolean showButton();
	
	/**
	 * 初始化界面
	 */
	public abstract void initView();
	
	/**
	 * 数据设置
	 */
    public abstract void initDatas();
	
    // 用于“待处理订单”的选项数据
    public List<List<Map<String,String>>> listDatas = new ArrayList<List<Map<String,String>>>();
    
    // 初衷是用于放listview的容器，还可以容纳其他控件
    public LinearLayout cornerContainer = null;
    
    // Order的数组数据，用在listview的adapter中
    public ArrayList<Order> orderList = new ArrayList();
	
    // “订单”listview，包括“待处理”，“待配送”，“待入帐”
    public ListView orderListview = null;
    
    protected boolean getState()  {
      return this.mState;
    }
    
	public void initTitleBar() {
		mLeftBtn = (Button) findViewById(R.id.buttonLeft);
		mTextViewTitle = (TextView) findViewById(R.id.textViewTitle);
		mTextViewTitle.setText(getTitleName());

		int cansee = showButton()?View.VISIBLE:View.GONE;
		mLeftBtn.setVisibility(cansee);
		
		if (this.mLeftBtn != null && cansee == View.VISIBLE)
		    this.mLeftBtn.setText(getLeftBtnName());
			
			//为“返回”按钮添加事件
			if(mLeftBtn.getText().equals("返回"))
				mLeftBtn.setOnClickListener(new OnClickListener() {
		            @Override
		            public void onClick(View v) {
		                onBackPressed();
		            }
		        });

		initView();
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		this.mContext = this;
		requestList = new ArrayList<BaseRequest>();
        sAllActivity.add(this);  
        
		sp = new BaseSharedPreferences(this);
		accessToken = sp.getString(SPConfig.ACCESSTOKEN, "0");
		
		
		super.onCreate(savedInstanceState);
	}

	public void setContentView(int paramInt) {
		super.setContentView(paramInt);
		initTitleBar();
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onPause()
	 */
	@Override
	protected void onPause() {
		/**
		 * 在activity销毁的时候同时设置停止请求，停止线程请求回调
		 */
		cancelRequest();
		super.onPause();
	    this.mState = false;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onDestroy()
	 */
	@Override
	protected void onDestroy() {
		/**
		 * 在activity销毁的时候同时设置停止请求，停止线程请求回调
		 */
		cancelRequest();
        sAllActivity.remove(this);  
		super.onDestroy();
	}

	@Override
	protected void onResume() {
		super.onResume();
	    this.mState = true;
	}

	private void cancelRequest() {
		if (requestList != null && requestList.size() > 0) {
			for (BaseRequest request : requestList) {
				if (request.getRequest() != null) {
					try {
						request.getRequest().abort();
						requestList.remove(request.getRequest());
						Log.d("netlib", "netlib ,onDestroy request to  "
								+ request.getRequest().getURI()
								+ "  is removed");
					} catch (UnsupportedOperationException e) {
						//do nothing .
					}
				}
			}
		}
	}
	
	/**
	 * 退出程序
	 */
	public void exit() {  
        for(Activity activity : sAllActivity) {  
            activity.finish();  
        }  
          
        sAllActivity.clear();  
    } 
	
	/**
	 * 返回特定activity, 并关闭其他所有的activity
	 * @param activity 返回的activity
	 */
	public void exit(Context context, Class<?> paramActivity) {
		
        for(Activity activity : sAllActivity) {  
            activity.finish();  
        }  
          
        sAllActivity.clear();  

		Intent mIntent = new Intent();
		mIntent.setClass(context, paramActivity);
		startActivity(mIntent);
    } 
	
}
